package com.mingke.common.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingke.common.entity.GroupEntity;
import com.mingke.common.entity.GroupUserRelateEntity;
import com.mingke.common.entity.UserEntity;
import com.mingke.common.mapper.GroupDao;
import com.mingke.common.mapper.GroupUserRelateDao;
import com.mingke.common.mapper.UserDao;
import com.mingke.common.service.GroupService;
import com.mingke.common.transfer.CreateGroupTO;
import com.mingke.common.transfer.JoinGroupTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("groupService")
public class GroupServiceImpl extends ServiceImpl<GroupDao, GroupEntity> implements GroupService {

    @Autowired
    private GroupDao groupDao;
    @Autowired
    private GroupUserRelateDao relateDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Integer create(CreateGroupTO createGroupTO) {
        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setGroupName(createGroupTO.getGroupName());
        groupEntity.setCreateTime(new Date());
        groupEntity.setUpdateTime(new Date());
        //获取到当前登陆的用户
        String uuid = (String) StpUtil.getLoginId();
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("user_uuid", uuid);
        List<UserEntity> userList = userDao.selectList(userEntityQueryWrapper);
        if(userList.size() != 1) {
            return -1;  //账户存在问题
        }else {
            //正常创建群组
            groupEntity.setCreateUser(userList.get(0).getUserId());
            groupEntity.setOwner(userList.get(0).getUserId());
            groupDao.insert(groupEntity);
            return groupEntity.getGroupId();
        }
    }

    @Override
    public Integer join(JoinGroupTO joinGroupTO) {
        //获取到当前登陆的用户
        String uuid = (String) StpUtil.getLoginId();
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("user_uuid", uuid);
        List<UserEntity> userList = userDao.selectList(userEntityQueryWrapper);
        if(userList.size() != 1) {
            return -1;  //账户存在问题
        }else {
            //加入群组
            GroupUserRelateEntity relateEntity = new GroupUserRelateEntity();
            relateEntity.setGroupId(joinGroupTO.getGroupID());
            relateEntity.setUserId(userList.get(0).getUserId());
            return relateDao.insert(relateEntity);
        }
    }

    @Override
    public Boolean isExist(Integer groupID) {
        QueryWrapper<GroupEntity> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("group_id", groupID);
        Integer count = groupDao.selectCount(groupQueryWrapper);
        return count == 1;
    }

    @Override
    public List<UserEntity> mates(Integer groupID) {
        //根据群组ID获取到当前群组的用户IDs
        QueryWrapper<GroupUserRelateEntity> relateMateWrapper = new QueryWrapper<>();
        relateMateWrapper.eq("group_id", groupID);
        List<GroupUserRelateEntity> relateEntityList = relateDao.selectList(relateMateWrapper);
        //根据用户ID查询到用户信息的集合
        List<UserEntity> relateMates = new ArrayList<>();
        for(GroupUserRelateEntity relateEntity : relateEntityList) {
            relateMates.add(userDao.selectById(relateEntity.getUserId()));
        }
        return relateMates;
    }
}